package org.adorsys.aderp.aderplogin.roles;

import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.security.access.ConfigAttribute;

public class ContextualWebExpressionConfigAttribute implements ConfigAttribute {

	private static final long serialVersionUID = 1L;
	private static final String ORG_SPRINGFRAMEWORK_SECURITY_WEB_ACCESS_EXPRESSION_WEB_EXPRESSION_CONFIG_ATTRIBUTE = "org.springframework.security.web.access.expression.WebExpressionConfigAttribute";
	private final Expression authorizeExpression;

    public ContextualWebExpressionConfigAttribute(Expression authorizeExpression) {
        this.authorizeExpression = authorizeExpression;
    }

    Expression getAuthorizeExpression() {
        return authorizeExpression;
    }

	@Override
    public String getAttribute() {
        return null;
    }

    @Override
    public String toString() {
        return authorizeExpression.getExpressionString();
    }
    
    public static boolean isWebExpressionConfigAttribute(ConfigAttribute configAttribute){
    	return configAttribute.getClass().getName().equals(ORG_SPRINGFRAMEWORK_SECURITY_WEB_ACCESS_EXPRESSION_WEB_EXPRESSION_CONFIG_ATTRIBUTE);
    }

    public static ContextualWebExpressionConfigAttribute newInstance(
    		ConfigAttribute configAttribute, SpelExpressionParser expressionParser)
    {
    	String expressionString = configAttribute.toString();
    	Expression parseExpression = expressionParser.parseExpression(expressionString);
    	return new ContextualWebExpressionConfigAttribute(parseExpression);
    }
}
